import Vue from 'vue'
import VueRouter from 'vue-router'

// import Login from '../components/Login.vue'
const Login = () => import(/* webpackChunkName:"login_home_welcome" */ '../components/Login.vue')
// import Home from '../components/Home.vue'
const Home = () => import(/* webpackChunkName:"login_home_welcome" */ '../components/Home.vue')

// import Welcome from '../components/childrenComponent/Welcome.vue'
const Welcome = () => import(/* webpackChunkName:"login_home_welcome" */ '../components/childrenComponent/Welcome.vue')

// import Users from '../components/childrenComponent/user/Users.vue'
const Users = () => import(/* webpackChunkName:"user" */ '../components/childrenComponent/user/Users.vue')

// import Rights from '../components/childrenComponent/power/Rights.vue'
const Rights = () => import(/* webpackChunkName:"power" */ '../components/childrenComponent/power/Rights.vue')

// import Roles from '../components/childrenComponent/power/Roles.vue'
const Roles = () => import(/* webpackChunkName:"power" */ '../components/childrenComponent/power/Roles.vue')

// import Cates from '../components/childrenComponent/goods/Cates.vue'
const Cates = () => import(/* webpackChunkName:"goods" */ '../components/childrenComponent/goods/Cates.vue')

// import Params from '../components/childrenComponent/goods/Params.vue'
const Params = () => import(/* webpackChunkName:"goods" */ '../components/childrenComponent/goods/Params.vue')

// import goodList from '../components/childrenComponent/goods/List.vue'
const goodList = () => import(/* webpackChunkName:"goods" */ '../components/childrenComponent/goods/List.vue')

// import Add from '../components/childrenComponent/goods/Add.vue'
const Add = () => import(/* webpackChunkName:"goods" */ '../components/childrenComponent/goods/Add.vue')

// import Order from '../components/childrenComponent/orders/Order.vue'
const Order = () => import(/* webpackChunkName:"orders" */ '../components/childrenComponent/orders/Order.vue')

// import Report from '../components/childrenComponent/reports/Reports.vue'
const Report = () => import(/* webpackChunkName:"reports" */ '../components/childrenComponent/reports/Reports.vue')

Vue.use(VueRouter)

const routes = [{
  path: '/',
  redirect: '/login'
},
{
  path: '/login',
  component: Login
},
{
  path: '/home',
  component: Home,
  redirect: '/welcome',
  children: [
    {
      path: '/welcome',
      component: Welcome
    },
    // 用户列表 home内部显示 内部子组件 children
    {
      path: '/users',
      component: Users
    },
    // 权限列表 home内部显示 内部子组件 children
    {
      path: '/rights',
      component: Rights
    },
    // 角色权限组件
    {
      path: '/roles',
      component: Roles
    },
    // 商品管理
    {
      path: '/categories',
      component: Cates
    },
    // 分类参数
    {
      path: '/params',
      component: Params
    },
    // 商品列表
    {
      path: '/goods',
      component: goodList
    },
    // 商品添加组件
    {
      path: '/goods/addProductPage',
      component: Add
    },
    // 订单列表
    {
      path: '/orders',
      component: Order
    },
    // 数据报表
    {
      path: '/reports',
      component: Report
    }
  ]
}
]

const router = new VueRouter({
  routes
})

// 路由判断是否登陆
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  // 获取token
  const tokenStr = sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  // 已登陆 放行
  next()
})

export default router
